package com.manager.customer.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.manager.customer.mapper.CustomerMapper;
import com.manager.dto.CustomerDto;
import com.manager.model.Customer;
import com.manager.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

public class CustomerListener extends AnalysisEventListener<CustomerDto> {

    private CustomerMapper customerMapper;
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private  Date beginTime;
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date endTime;

    public CustomerListener(CustomerMapper customerMapper, Date beginTime, Date endTime) {
        this.customerMapper = customerMapper;
        this.beginTime = beginTime;
        this.endTime = endTime;
    }

    @Override
    public void invoke(CustomerDto customerDto, AnalysisContext analysisContext) {
        //调用方法添加数据库
        Customer customer = new Customer();
        String phone = customerDto.getPhone();
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Customer::getPhone,phone);
        Customer selectOne = customerMapper.selectOne(wrapper);
        BeanUtils.copyProperties(customerDto,customer);
        if (customer.getCreateTime().after(beginTime) && customer.getCreateTime().before(endTime) && selectOne == null){
            customerMapper.insert(customer);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
